package com.onpositive.wikidataeditor;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.eclipse.jface.action.Action;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IWorkbenchWindow;

import com.onpositive.mediawiki.propertyparser.ParsedCategory;
import com.onpositive.mediawiki.serializing.WriterFactory;
import com.onpositive.data.serializer.writers.ObjectCollectionWriter ;
import com.onpositive.mediawiki.serializing.UniverseObjectCollection ;

public class SerializeAction extends Action
{	
	private final IWorkbenchWindow window;
	private final String viewId;
	
	public SerializeAction(IWorkbenchWindow window, String label, String viewId) {
		this.window = window;
		this.viewId = viewId;
        setText(label);
        // The id is used to refer to the action in a menu or toolbar
		setId(ICommandIds.CMD_SERIALIZE);
        // Associate the action with a pre-defined command, to allow key bindings.
		setActionDefinitionId(ICommandIds.CMD_SERIALIZE);
		setImageDescriptor(com.onpositive.wikidataeditor.Activator.getImageDescriptor("/icons/sample2.gif"));
	}
	
	public void run() {
		if(window != null) {	

				//window.getActivePage().showView(viewId, Integer.toString(instanceNum++), IWorkbenchPage.VIEW_ACTIVATE);
				FileDialog fd = new FileDialog( window.getShell(), SWT.OPEN);
		        fd.setText("Serialize");
		        fd.setFilterPath("C:/wikistorage/!serialized");
		        String[] filterExt = { "*.dat" };
		        fd.setFilterExtensions( filterExt );
		        String path = fd.open();
		        
		        File file = new File( path ) ;
		        
		        DataOutputStream stream ;		        
		        try {
					stream = new DataOutputStream( new FileOutputStream( file ) );
				} catch (FileNotFoundException e) {
					e.printStackTrace();
					return ;
				}
       
		        ParsedCategory pc = Resources.getInstance().getWorkingCategory() ;
		        
//		        ObjectCollectionWriter ocw = new ObjectCollectionWriter() ;
//		        ocw.setFactory( new WriterFactory() );
//		        ocw.append( new CategoryObjectCollection(pc) ) ;
//		        try {
//					ocw.serialize(stream) ;
//				} catch (IOException e) {
//					e.printStackTrace();
//					return ;
//				}
		 }	
	}
}
